package com.sdtele.tta.operation;


import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.TaskOperation;
import com.sdtele.tta.models.TaskResponse;
import com.sdtele.tta.models.User;


/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationSetFeedback extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
	
		int action = jsonRequest.getJsonObject().get("action").getAsInt();
		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
			return jres;
			
		}
		long userId = curUser.getId();
		
		if (action == 1) {
			String feedBack=jsonRequest.getJsonObject().get("feedBack").getAsString();
			Timestamp responseTime = new Timestamp(System.currentTimeMillis());
			long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
			
			TaskOperation op = Dac.getInstance().getOperation(taskId, userId);
			if (op != null) {
				if (op.getOperation() > 2) {
					jres.setStatus(1);
					jres.setMessage("用户为该项目分阅人，不允许添加反馈");
					return jres;
				}
			 else {
				TaskResponse taskRp = new TaskResponse();
				taskRp.setTaskId(taskId);
				taskRp.setUserId(userId);
				taskRp.setResponse( feedBack);
				taskRp.setResponseTime(responseTime);
				TaskResponse	newFeedback = Dac.getInstance().addResponse(taskRp);
				if (newFeedback.getId() <= 0) {
					jres.setStatus(1);
					jres.setMessage("添加反馈失败");
					return jres;
				}else{
					
					Map<String,Object> map = new HashMap<String,Object>();
			    //List<TaskResponse> newFeedbackList = Dac.getInstance().getResponse(taskId, userId);
				//TaskResponse	newFeedback = newFeedbackList.get(newFeedbackList.size()-1);
				//	System.out.println(newFeedback);
					map.put("feedBack", newFeedback.ToTaskResponse());
					User feedBackUser = Dac.getInstance().getUser(newFeedback.getUserId());
					map.put("feedBackUser", feedBackUser);
				jres.setStatus(0);
				jres.setMessage("添加反馈成功");
				jres.setEntity(map);
			}
		}
	}else {
			jres.setStatus(1);
			jres.setMessage("用户不是该任务的相关人员");
		}
	}
	if (action == 0) {
		
		    long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
		    long feedbackId =jsonRequest.getJsonObject().get("feedBackId").getAsLong();
			TaskOperation op = Dac.getInstance().getOperation(taskId, userId);
			if (op != null) {
				if (op.getOperation() == 2) {
					jres.setStatus(1);
					jres.setMessage("用户为该项目分阅人，不允许删除反馈");
					return jres;
				}
			 else {
				if (Dac.getInstance().delTaskResponsebyId(feedbackId)) {
					jres.setStatus(0);
					jres.setMessage("删除反馈成功");
					jres.setEntity(feedbackId);
					return jres;
				}else{
				jres.setStatus(1);
				jres.setMessage("删除反馈成功");
			}
		}
	}else {
			jres.setStatus(1);
			jres.setMessage("用户不是该任务的相关人员");
		}
	}
		return jres;
	}
   
}
